﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jitter.Dynamics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using X13.Libs;

namespace X13.Mapping
{
    public sealed class MineTile : ISelectable3D
    {
        //Fields
        private CellPrimative _CellPrimative;
        private FencePrimitive _FencePrimitive;
        private SpiralPrimitive _SpiralPrimitive;

        public MineState MineState { get; set; }
        public MinfieldColourState ColourState { get; set; }

        //Construct
        public MineTile(MinefieldPosition position)
        {
            this.ColourState = (MinfieldColourState)Globals.RandonGen.Next(1,4);
            this.MineState = Mapping.MineState.Hidden;

            _CellPrimative = new CellPrimative(position);
            _FencePrimitive = new FencePrimitive(position);
            _SpiralPrimitive = new SpiralPrimitive(position);
        }

        //Methods
        public void UpdateCell(GameTime time)
        {
            _SpiralPrimitive.Rotate(0, MathHelper.ToRadians(5), 0);
        }
        public void RenderCell()
        {
            bool DrawFence = true;
            bool DrawSpiral = true;

            #region Cell Base
            Matrix[] transforms = new Matrix[Resources.MinefieldModels[ModelType.CellBase].Bones.Count];
            Resources.MinefieldModels[ModelType.CellBase].CopyAbsoluteBoneTransformsTo(transforms);
            foreach (ModelMesh mesh in Resources.MinefieldModels[ModelType.CellBase].Meshes)
            {
                foreach (Effect fx in mesh.Effects)
                {
                    fx.CurrentTechnique = fx.Techniques["TextureShade"];

                    fx.Parameters["World"].SetValue(transforms[mesh.ParentBone.Index] * _CellPrimative.World);
                    fx.Parameters["View"].SetValue(ObjectHolder.GameCamera.View);
                    fx.Parameters["Projection"].SetValue(ObjectHolder.GameCamera.Projection);
                    fx.Parameters["Texture"].SetValue(Resources.MinefieldTexturedLibary["Grass"]);
                    fx.Parameters["AmbientColor"].SetValue(Globals.AmbientLightColor.ToVector4());
                    fx.Parameters["AmbientLevel"].SetValue(Globals.AmbientLightLevel);
                    fx.Parameters["IsHovering"].SetValue(this.SelectionState == SelectableState.Hover);
                }
                mesh.Draw();
            }
            #endregion

            #region Fence
            if (DrawFence)
            {
                transforms = new Matrix[Resources.MinefieldModels[ModelType.CellFence].Bones.Count];
                Resources.MinefieldModels[ModelType.CellFence].CopyAbsoluteBoneTransformsTo(transforms);
                foreach (ModelMesh mesh in Resources.MinefieldModels[ModelType.CellFence].Meshes)
                {
                    foreach (Effect fx in mesh.Effects)
                    {
                        fx.CurrentTechnique = fx.Techniques["ColourShade"];

                        fx.Parameters["World"].SetValue(transforms[mesh.ParentBone.Index] * _FencePrimitive.World);
                        fx.Parameters["View"].SetValue(ObjectHolder.GameCamera.View);
                        fx.Parameters["Projection"].SetValue(ObjectHolder.GameCamera.Projection);
                        fx.Parameters["Texture"].SetValue(Resources.FenceTexture);

                        //TODO:
                        fx.Parameters["Colour"].SetValue(Resources.MinefieldColourDictionary[ColourState].ToVector4());
                        fx.Parameters["IsHovering"].SetValue(this.SelectionState == SelectableState.Hover);

                        fx.Parameters["AmbientColor"].SetValue(Globals.AmbientLightColor.ToVector4());
                        fx.Parameters["AmbientLevel"].SetValue(Globals.AmbientLightLevel);
                    }
                    mesh.Draw();
                }
            }
            #endregion

            #region Spiral
            if (DrawSpiral)
            {
                transforms = new Matrix[Resources.MinefieldModels[ModelType.CellMineSpiral].Bones.Count];
                Resources.MinefieldModels[ModelType.CellMineSpiral].CopyAbsoluteBoneTransformsTo(transforms);
                foreach (ModelMesh mesh in Resources.MinefieldModels[ModelType.CellMineSpiral].Meshes)
                {
                    foreach (Effect fx in mesh.Effects)
                    {
                        fx.CurrentTechnique = fx.Techniques["ColourShade"];

                        fx.Parameters["World"].SetValue(transforms[mesh.ParentBone.Index] * _SpiralPrimitive.World);
                        fx.Parameters["View"].SetValue(ObjectHolder.GameCamera.View);
                        fx.Parameters["Projection"].SetValue(ObjectHolder.GameCamera.Projection);
                        fx.Parameters["Texture"].SetValue(Resources.MineSpiralTexture);

                        //TODO:
                        fx.Parameters["Colour"].SetValue(Resources.MinefieldColourDictionary[ColourState].ToVector4());
                        fx.Parameters["IsHovering"].SetValue(this.SelectionState == SelectableState.Hover);

                        fx.Parameters["AmbientColor"].SetValue(Globals.AmbientLightColor.ToVector4());
                        fx.Parameters["AmbientLevel"].SetValue(Globals.AmbientLightLevel);
                    }
                    mesh.Draw();
                }
            }
            #endregion
        }
        
        #region ISelectable Interface
        public RigidBody Body
        {
            get { return _CellPrimative; }
        }
        public MinefieldPosition Position
        {
            get { return _CellPrimative.Position; }
        }
        public SelectableState SelectionState { get;  set;  }
        #endregion
    }
}
